/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import dao.Dao;
import entidades.Artista;
import entidades.Musica;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Time;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author aluno
 */
@WebServlet(name = "ServletMusica", urlPatterns = {"/ServletMusica"})
public class ServletMusica extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ServletMusica</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet ServletMusica at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
             */
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        String op = request.getParameter("op");
        Dao<Musica> daoa = new Dao<Musica>(Musica.class);
        List<Musica> arrayMusica = ((Artista) session.getAttribute("Artista")).getMusicas();
        List<Musica> arrayMusica2 = new ArrayList<Musica>();
        int id = Integer.parseInt((String) request.getParameter("id"));
        if (op.equals("list")) {
            Musica m = new Musica();
            for (int i = 10; i < arrayMusica.size(); i++) {
                arrayMusica2.add(arrayMusica.get(i));
            }

        } else if (op.equals("remover")) {
            Dao<Musica> dao = new Dao<Musica>(Musica.class);
            dao.remove(id);

        }
        session.setAttribute("musicas", arrayMusica2);
        RequestDispatcher rd = request.getRequestDispatcher("gerenciarMusica.jsp");
        rd.forward(request, response);
    }
    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    public final String dir = "/files/";

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Upload upload = new Upload(getServletContext().getRealPath(dir));

        // Retorna um list com todos os componentes de uma requisição
        List list1 = upload.processRequest(request);

        // Neste Map você obteve todos os campos do formulário.
        Map<String, String> map = upload.getFormValues(list1);
        String op = map.get("op");

        String nome = map.get("nome");
        String time = map.get("duracao");
        String foto = map.get("foto");
        Dao<Musica> daoa = new Dao<Musica>(Musica.class);

        ArrayList<Integer> array = new ArrayList();
        //String nome = (String) request.getParameter("nome");

        // String time = (String) request.getParameter("duracao");

        Musica m = new Musica();

        m.setNome(nome);
        m.setDuracao(Time.valueOf(time));
        m.setFoto(foto);
        if (op.equals("cadastrar")) {



            daoa.insert(m);
            RequestDispatcher rd = request.getRequestDispatcher("UsuarioSuper.jsp");
            rd.forward(request, response);
        } else if (op.equals("pesquisar2")) {
            HttpSession session = request.getSession();
            List<Musica> arrayMusica = ((Artista) session.getAttribute("Artista")).getMusicas();
            List<Musica> arMusicas2 = new ArrayList<Musica>();
            String nomeBuscar = request.getParameter("buscar");
            for (Musica musica : arrayMusica) {
                if (musica.getNome().contains(nomeBuscar)) {
                    arMusicas2.add(musica);
                }
            }


            session.setAttribute("musicas", arMusicas2);
            RequestDispatcher rd = request.getRequestDispatcher("gerenciarMusica.jsp");
            rd.forward(request, response);
        } else if (op.equals("update")) {
            daoa.update(m);
            RequestDispatcher rd = request.getRequestDispatcher("UsuarioSuper.jsp");
            rd.forward(request, response);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
